package cn.pping.org.service.impl;

import cn.pping.basic.exception.BusinessExeption;
import cn.pping.basic.util.BaiduAuditUtils;
import cn.pping.org.domain.Employee;
import cn.pping.org.domain.Shop;
import cn.pping.org.domain.ShopAuditLog;
import cn.pping.org.domain.ShopDto;
import cn.pping.org.mapper.EmployeeMapper;
import cn.pping.org.mapper.ShopAuditLogMapper;
import cn.pping.org.mapper.ShopMapper;
import cn.pping.org.service.IShopService;
import cn.pping.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author pping
 * @since 2022-07-24
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {
    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;

    @Autowired
    private JavaMailSender javaMailSender;

    @Override
    public void settlement(Shop shop) {
        // 店铺入驻流程分析
        // 判断参数是否为空
        // 店员信息
        Employee admin = shop.getAdmin();
        if (StringUtils.isEmpty(shop.getAddress()) ||
                StringUtils.isEmpty(shop.getTel()) ||
                StringUtils.isEmpty(admin.getUsername()) ||
                StringUtils.isEmpty(admin.getPhone()) ||
                StringUtils.isEmpty(admin.getEmail()) ||
                StringUtils.isEmpty(admin.getPassword()) ||
                StringUtils.isEmpty(admin.getComfirmPassword())

        ) {
            throw new BusinessExeption("参数不能为空");
        }

        // 通过百度AI审核名称是否符合要求
        Boolean aBoolean = BaiduAuditUtils.TextCensor(shop.getName());
        if (!aBoolean) { // 名称有问题
            throw new BusinessExeption("店铺名称违规请修改");
        }
        // 判断两次密码是否一致
        if (!admin.getPassword().equals(admin.getComfirmPassword())) {
            throw new BusinessExeption("两次密码不一致请重新输入");
        }
        // 判断店铺是否存在
        Shop dbShop = shopMapper.getShopByMessage(shop);
        if (dbShop != null) {
            throw new BusinessExeption("该店铺已经注册过");
        }
        // 把shop的字段和emp的字段保存进表中开启主键返回
        // 先把保存emp 传入admin
        employeeMapper.add(admin);
        // 在把返回的主键id 保存进shop的adminId
        // 保存shop
        shopMapper.add(shop);
        // 在将返回的主键给到emp的shopId
        // 修改
        admin.setShop_id(shop.getId());
        // 更新emp
        employeeMapper.update(admin);


    }

    /**
     * @Title: pass 通过后的接口
     * @Description: TODO
     * @Author: Pan
     * @Version: 1.0
     * @Date: 2022/7/26 21:40
     * @Parameters: [log]
     * @Return void
     */

    @Override
    public void pass(ShopAuditLog log) throws MessagingException {
        // 通过从log里面获取参数保存到log中
        // 修改店铺状态 通过shop id 找到对应的shop店铺
        Shop shop = shopMapper.loadById(log.getShopId());
        // 得到shop该数据字段后将字段设置为 店铺状态：0待审核，1审核通过，-1驳回，2待激活
        shop.setState(1);
        // 将结果保存到shop中
        shopMapper.update(shop);

        // 录入日志 从Log中获取日志中写修改状态为1
        log.setState(1);
        // 修改日志
        log.setAuditId(1L);
        // 更新
        shopAuditLogMapper.update(log);
        // 发送邮件
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "utf-8");
        helper.setFrom("1520931171@qq.com");
        helper.setSubject("通过邮件");
        helper.setText("<h1>你的店铺已经注册成功!!!</h1><a href='http://localhost:8080/shop//"+log.getShopId()+"'>点击该链接激活</a>",true);
        // 获取收件人
//        Employee employee = employeeMapper.loadById(shop.getAdminId());
//        String email = employee.getEmail();
        helper.setTo("1120707667@qq.com");
        // 发送
        javaMailSender.send(mimeMessage);
    }

    @Override
    public void reject(ShopAuditLog log) throws MessagingException {
        // 1. 修改shop状态
        // 通过查询shop数据再修改状态
        Shop shop = shopMapper.loadById(log.getShopId());
        // 审核通过
        shop.setState(-1);
        shopMapper.update(shop);

        // 2. 录入日志  shopLog
        log.setState(-1);
        log.setAuditId(1L);
        shopAuditLogMapper.add(log);

        // 3. 通知商家   邮件发送
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        //发送复杂邮件的工具类
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
        helper.setFrom("1520931171@qq.com");
        helper.setSubject("驳回邮件");
        helper.setText("<h1>你的店铺入驻失败!!!</h1><a href='http://localhost:8080/shop/active/"+log.getShopId()+"'>点击该链接激活</a>",true);

        // 获取收件人
        // 这是写活的时候
//        Employee employee = employeeMapper.loadById(shop.getAdminId());
//        String email = employee.getEmail();
        helper.setTo("1120707667@qq.com");
        // 发送
        javaMailSender.send(mimeMessage);
    }

    @Override
    public HashMap<String, Object> loadShopEchartsData() {
        List<ShopDto> shopVos = shopMapper.loadShopEchartsData();
        List<Integer> x = shopVos.stream().map(ShopDto::getState).collect(Collectors.toList());
        List<Integer> y = shopVos.stream().map(ShopDto::getCountNum).collect(Collectors.toList());
        HashMap<String, Object> data = new HashMap<>();
        data.put("x",x);
        data.put("y",y);
        return data;
    }
}
